package com.changgou.search.service.Impl;

import com.alibaba.fastjson.JSON;
import com.changgou.entity.ResultCode;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.search.dao.ESMangerMapper;
import com.changgou.search.exception.MyBaseException;
import com.changgou.search.pojo.Skuinfo;
import com.changgou.search.service.EsMangerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: 大佬味的小男孩
 * @date: 2020-07-22 21:00
 **/
@Service
public class EsMangerServiceImpl implements EsMangerService {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private ESMangerMapper esMangerMapper;

    //创建索引库
    @Override
    public void createMappingAndIndex() {
        //根据映射实体类来创建索引库
        elasticsearchTemplate.createIndex(Skuinfo.class);
        //根据映射实体类来创建映射
        elasticsearchTemplate.putMapping(Skuinfo.class);

        System.out.println("创建成功");
    }

    //导入全部sku数据进入es
    @Override
    public void importAll() {
        //查询sku所有数据
        List<Sku> skuList = skuFeign.findSkuListByspuid("all");
        if (skuList == null || skuList.size() <= 0) {
            MyBaseException.throwe(ResultCode.DATA_NOT_EXIST);
        }
        //skulist转换为json
        String skuJson = JSON.toJSONString(skuList);
        //json转化成skuinfo
        List<Skuinfo> skuinfoList = JSON.parseArray(skuJson, Skuinfo.class);

        //遍历集合 将规格信息装成map
        for (Skuinfo skuinfo : skuinfoList) {
            System.out.println("规格信息:" + skuinfo.getSpec());
            Map map = JSON.parseObject(skuinfo.getSpec(), Map.class);
            skuinfo.setSpecMap(map);
        }
        //导入索引库
        esMangerMapper.saveAll(skuinfoList);
    }


    //根据spuid查询skuList,再导入索引库
    @Override
    public void importDataBySpuId(String spuId) {
        //根据spuid查询skuList
        List<Sku> skuList = skuFeign.findSkuListByspuid(spuId);

        if (skuList == null || skuList.size() <= 0) {
            MyBaseException.throwe(ResultCode.DATA_NOT_EXIST);
        }
        //skulist转换为json
        String skuJson = JSON.toJSONString(skuList);
        //json转化成skuinfo
        List<Skuinfo> skuinfoList = JSON.parseArray(skuJson, Skuinfo.class);
        //遍历集合 将规格信息装成map
        for (Skuinfo skuinfo : skuinfoList) {
            System.out.println("规格信息:" + skuinfo.getSpec());
            Map map = JSON.parseObject(skuinfo.getSpec(), Map.class);
            skuinfo.setSpecMap(map);
        }
        //导入索引库
        esMangerMapper.saveAll(skuinfoList);
    }


    //根据spuid删除es索引库中相关的sku数据
    @Override
    public void delDataBySpuId(String spuId) {
        //根据spuid查询skuList
        List<Sku> skuList = skuFeign.findSkuListByspuid(spuId);
        if (skuList == null || skuList.size() <= 0) {
            MyBaseException.throwe(ResultCode.DATA_NOT_EXIST);
        }
        for (Sku sku : skuList) {
            String id = sku.getId();
            esMangerMapper.deleteById(Long.parseLong(id));
        }

    }
}
